package com.example.room.controller;

import com.example.core.annotation.HasPerm;
import com.example.model.response.Result;
import com.example.model.room.Room;
import com.example.room.service.RoomService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/room")
public class RoomController {

    @Autowired
    private RoomService roomService;

    @GetMapping("/{id}")
    public Result get(@PathVariable("id") Long id) {
        return Result.success(roomService.get(id));
    }

    @GetMapping("getByNo/{roomNo}")
    public Result getByNo(@PathVariable("roomNo") String roomNo) {
        return Result.success(roomService.getByNo(roomNo));
    }

    @HasPerm("management:room:list")
    @GetMapping("/list")
    public Result list(@RequestBody Room room) {
        return Result.success(roomService.list(room));
    }

    @HasPerm("management:room:list")
    @GetMapping("/page")
    public Result page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, Room room) {
        Page<Room> page = PageHelper.startPage(pageNum, pageSize);
        List<Room> list = roomService.list(room);
        PageInfo<Room> pageInfo = new PageInfo<>(page);
        return Result.success(pageInfo);
    }

    @HasPerm("management:room:insert")
    @PostMapping
    public Result insert(@Valid @RequestBody Room room) {
        return Result.success(roomService.insert(room) == 1 ? "新增成功" : "新增失败");
    }

    @HasPerm("management:room:update")
    @PutMapping
    public Result update(@Valid @RequestBody Room room) {
        return Result.success(roomService.update(room) == 1 ? "更新成功" : "更新失败");
    }

    @HasPerm("management:room:delete")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id) {
        return Result.success(roomService.delete(id) == 1 ? "删除成功" : "删除失败");
    }

    @GetMapping("/enable")
    public Result enableCheckIn(@RequestParam("checkInDate")String checkInDate) {
        return Result.success(roomService.enableCheckIn(checkInDate));
    }

}
